<?php
namespace app\common\model\assay;
use app\api\controller\Pub;
use app\common\model\user\User;
use app\common\model\power\Rolepower;
use think\Model;

//送样信息
class Assay extends Model
{
    protected $table = 'assay';
    protected $pk    = 'AssayId';

    public function goodinfo()
    {
        return $this->hasOne('app\common\model\orderdata\Goods', 'GoodId', 'GoodId');
    }

    /**
     * 创建化验信息的关联危废列表
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($id,$idata)
    {   
        $Assay               = new Assay();
        $add = [];
        $filed  = ['GoodId', 'GoodName','SubImg','SubNote'];
        foreach ($idata as $key => $value) {
            $add[$key]['AssayListId']  = $id;
            foreach ($filed as $fkey => $fvalue) {
                if (isset($value[$fvalue])) {
                    $add[$key][$fvalue] = $value[$fvalue];
                }
            }
        }
        $Assay->saveAll($add);
        return ;
    }

    /**
     * 获取列表
     * @param  [type] $id       [服务费记录表id]
     * @param  [type] $usertype [description]
     * @return [type]           [description]
     */
    public static function getlist($id){
        $data = Assay::where('AssayListId',$id)->with('goodinfo');
        $hideen = ['goodinfo'=>['TotalWeight','OperationLog']];
        if(!Rolepower::ispower(204016)){
            $hideen[] = 'OperationLog';
        }
        $data = $data->hidden($hideen);

        $data = $data->select();
        return $data->toArray();
    }



    /**
     * 用户修改信息
     * @param  [type]  $data   [description]
     * @param  boolean $wtnote [是否记录]
     * @param  boolean $nochangemsg [没有变动的话是否提示]
     * @return [type]          [description]
     */
    public static function user_reinfo($data, $wtnote = true,$nochangemsg = true)
    {
        if (empty($data['id'])) {
            return moerr('缺少重要参数');
        }
        $info     = Assay::get($data['id']);
        $userinfo = User::getinfo();
        $reinfo   = empty($info->OperationLog) ? '' : $info->OperationLog . "\n";
        $reinfo   = $reinfo . '【' . $userinfo->Name . '|' . $userinfo->UserId . '】在' . date('Y-m-d H:i', time()) . '进行了信息调整：';
        $nreinfo  = '';
        // dump($data);
        foreach ($data as $key => $value) {
            $sb = $info->getfiledname($key);
            if (empty($sb)) {
                continue;
            }
            //取原数据
            $ovalue = $info->$key;
            if ($key == 'State' && $value !== $ovalue) {
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value   = $info->getStatusText((int) $value);
                $ovalue  = $info->getStatusText((int) $ovalue);
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            }  else if ((strpos($key, "Time") !== false) ) {    
                //过滤掉时间相同的数据  
                if(strtotime($ovalue) !== strtotime($value)){
                    $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                    $info->$key = $value;
                }
            } else if (isset($value) && $ovalue !== $value) {                
                $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                $info->$key = $value;
            }
        }
        if (!empty($nreinfo)) {
            if ($wtnote) {
                $reinfo             = $reinfo . "\n" . $nreinfo;
                $info->OperationLog = $reinfo;
            }
            $info->save();
        } else {
            if($nochangemsg){
                return moerr('没有找到需要调整的数据');
            }
        }
    }

    /**
     * 获取字段的名字，同时仅可删除带名字的字段
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public static function getfiledname($str)
    {
        $filename = [
            'SubImg'=>'送审图片','SubNote'=>'送审备注',
            'AccImg'=>'接收图片','AccNote'=>'接收备注',
            'RegImg'=>'登记图片','RegNote'=>'登记备注',
            'GoodName' => '危废名称','Price'=>'危废单价',
        ];
        if (empty($filename[$str])) {
            return false;
        }
        return $filename[$str];
    }

}
